package com.uchengwang.ui.cm_ui.rychat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.image.ImageManager;

import io.rong.imkit.RongContext;
import io.rong.imkit.model.ConversationProviderTag;
import io.rong.imkit.model.UIConversation;
import io.rong.imkit.widget.ProviderContainerView;
import io.rong.imkit.widget.adapter.ConversationListAdapter;
import io.rong.imkit.widget.provider.IContainerItemProvider;
import io.rong.imlib.model.Conversation;

/**
 * Created by weiqinxiao on 15/11/5.
 */
public class ConversationListAdapterEx extends ConversationListAdapter {

    private Context mContext;
    private ConversationListAdapter.OnPortraitItemClick mOnPortraitItemClick;
    public ConversationListAdapterEx(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    protected View newView(Context context, int position, ViewGroup group) {
        View result = LayoutInflater.from(mContext).inflate(R.layout.rc_item_conversation_my, (ViewGroup)null);
        ViewHolder holder = new ViewHolder();
        holder.layout = result.findViewById(R.id.rc_item_conversation);
        holder.leftImageLayout = result.findViewById(R.id.rc_item1);
        holder.rightImageLayout = result.findViewById(R.id.rc_item2);
        holder.leftUnReadView = result.findViewById(R.id.rc_unread_view_left);
        holder.rightUnReadView = result.findViewById(R.id.rc_unread_view_right);
        holder.leftImageView = (ImageView)result.findViewById(R.id.rc_left);
        holder.rightImageView = (ImageView)result.findViewById(R.id.rc_right);
        holder.contentView = (ProviderContainerView)result.findViewById(R.id.rc_content);
        holder.unReadMsgCount = (TextView)result.findViewById(R.id.rc_unread_message);
        holder.unReadMsgCountRight = (TextView)result.findViewById(R.id.rc_unread_message_right);
        holder.unReadMsgCountIcon = (ImageView)result.findViewById(R.id.rc_unread_message_icon);
        holder.unReadMsgCountRightIcon = (ImageView)result.findViewById(R.id.rc_unread_message_icon_right);
        result.setTag(holder);
        return result;
    }

    @Override
    protected void bindView(View v, int position, final UIConversation data) {
        if (data != null) {
            if (data.getConversationType().equals(Conversation.ConversationType.DISCUSSION))
                data.setUnreadType(UIConversation.UnreadRemindType.REMIND_ONLY);
        }
        ViewHolder holder = (ViewHolder)v.getTag();
        if(data != null) {
            IContainerItemProvider.ConversationProvider provider = RongContext.getInstance().getConversationTemplate(data.getConversationType().getName());
            if(provider == null) {
            } else {
                View view = holder.contentView.inflate(provider);
                provider.bindView(view, position, data);
                if(data.isTop()) {
                    holder.layout.setBackgroundDrawable(this.mContext.getResources().getDrawable(R.drawable.rc_item_top_list_selector));
                } else {
                    holder.layout.setBackgroundDrawable(this.mContext.getResources().getDrawable(R.drawable.rc_item_list_selector));
                }

                ConversationProviderTag tag = RongContext.getInstance().getConversationProviderTag(data.getConversationType().getName());
                boolean defaultId = false;
                int defaultId1;
                if(data.getConversationType().equals(Conversation.ConversationType.GROUP)) {
                    defaultId1 = R.drawable.rc_default_group_portrait;
                } else if(data.getConversationType().equals(Conversation.ConversationType.DISCUSSION)) {
                    defaultId1 = R.drawable.rc_default_discussion_portrait;
                } else {
                    defaultId1 = R.drawable.no_head;
                }

                if(tag.portraitPosition() == 1) {
                    holder.leftImageLayout.setVisibility(View.VISIBLE);
                    holder.leftImageLayout.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            if(mOnPortraitItemClick != null) {
                                mOnPortraitItemClick.onPortraitItemClick(v, data);
                            }

                        }
                    });
                    holder.leftImageLayout.setOnLongClickListener(new View.OnLongClickListener() {
                        public boolean onLongClick(View v) {
                            if(mOnPortraitItemClick != null) {
                                mOnPortraitItemClick.onPortraitItemLongClick(v, data);
                            }

                            return true;
                        }
                    });
                    if(data.getConversationGatherState()) {
                        ImageManager.getInstance().loadImage(mContext, holder.leftImageView, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
                    } else if(data.getIconUrl() != null) {
                        ImageManager.getInstance().loadImage(mContext, holder.leftImageView, data.getIconUrl().toString(), ImageManager.ImageType.CIRCLE);
                    } else {
                        ImageManager.getInstance().loadImage(mContext, holder.leftImageView, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
                    }

                    if(data.getUnReadMessageCount() > 0) {
                        holder.unReadMsgCountIcon.setVisibility(View.VISIBLE);
                        this.setUnReadViewLayoutParams(holder.leftUnReadView, data.getUnReadType());
                        if(data.getUnReadType().equals(UIConversation.UnreadRemindType.REMIND_WITH_COUNTING)) {
                            if(data.getUnReadMessageCount() > 99) {
                                holder.unReadMsgCount.setText(this.mContext.getResources().getString(R.string.rc_message_unread_count));
                            } else {
                                holder.unReadMsgCount.setText(Integer.toString(data.getUnReadMessageCount()));
                            }

                            holder.unReadMsgCount.setVisibility(View.VISIBLE);
                            holder.unReadMsgCountIcon.setImageResource(R.drawable.rc_unread_count_bg);
                        } else {
                            holder.unReadMsgCount.setVisibility(View.GONE);
                            holder.unReadMsgCountIcon.setImageResource(R.drawable.rc_unread_remind_list_count);
                        }
                    } else {
                        holder.unReadMsgCountIcon.setVisibility(View.GONE);
                        holder.unReadMsgCount.setVisibility(View.GONE);
                    }

                    holder.rightImageLayout.setVisibility(View.GONE);
                } else if(tag.portraitPosition() == 2) {
                    holder.rightImageLayout.setVisibility(View.GONE);
                    holder.rightImageLayout.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            if(mOnPortraitItemClick != null) {
                                mOnPortraitItemClick.onPortraitItemClick(v, data);
                            }

                        }
                    });
                    holder.rightImageLayout.setOnLongClickListener(new View.OnLongClickListener() {
                        public boolean onLongClick(View v) {
                            if(mOnPortraitItemClick != null) {
                                mOnPortraitItemClick.onPortraitItemLongClick(v, data);
                            }

                            return true;
                        }
                    });
                    if(data.getConversationGatherState()) {
                        ImageManager.getInstance().loadImage(mContext, holder.rightImageView, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
                    } else if(data.getIconUrl() != null) {
                        ImageManager.getInstance().loadImage(mContext, holder.rightImageView, data.getIconUrl().toString(), ImageManager.ImageType.CIRCLE);
                    } else {
                        ImageManager.getInstance().loadImage(mContext, holder.rightImageView, R.drawable.no_head, ImageManager.ImageType.CIRCLE);
                    }

                    if(data.getUnReadMessageCount() > 0) {
                        holder.unReadMsgCountRightIcon.setVisibility(View.VISIBLE);
                        this.setUnReadViewLayoutParams(holder.rightUnReadView, data.getUnReadType());
                        if(data.getUnReadType().equals(UIConversation.UnreadRemindType.REMIND_WITH_COUNTING)) {
                            holder.unReadMsgCount.setVisibility(View.VISIBLE);
                            if(data.getUnReadMessageCount() > 99) {
                                holder.unReadMsgCountRight.setText(this.mContext.getResources().getString(R.string.rc_message_unread_count));
                            } else {
                                holder.unReadMsgCountRight.setText(Integer.toString(data.getUnReadMessageCount()));
                            }

                            holder.unReadMsgCountRightIcon.setImageResource(R.drawable.rc_unread_count_bg);
                        } else {
                            holder.unReadMsgCount.setVisibility(View.GONE);
                            holder.unReadMsgCountRightIcon.setImageResource(R.drawable.rc_unread_remind_without_count);
                        }
                    } else {
                        holder.unReadMsgCountIcon.setVisibility(View.GONE);
                        holder.unReadMsgCount.setVisibility(View.GONE);
                    }

                    holder.leftImageLayout.setVisibility(View.GONE);
                } else {
                    if(tag.portraitPosition() != 3) {
                        throw new IllegalArgumentException("the portrait position is wrong!");
                    }

                    holder.rightImageLayout.setVisibility(View.GONE);
                    holder.leftImageLayout.setVisibility(View.GONE);
                }

            }
        }
    }

    public void setOnPortraitItemClick(ConversationListAdapter.OnPortraitItemClick onPortraitItemClick) {
        this.mOnPortraitItemClick = onPortraitItemClick;
    }

    class ViewHolder {
        View layout;
        View leftImageLayout;
        View rightImageLayout;
        View leftUnReadView;
        View rightUnReadView;
        ImageView leftImageView;
        TextView unReadMsgCount;
        ImageView unReadMsgCountIcon;
        ImageView rightImageView;
        TextView unReadMsgCountRight;
        ImageView unReadMsgCountRightIcon;
        ProviderContainerView contentView;

        ViewHolder() {
        }
    }
}
